library(dplyr)
library(magrittr)
library(ggplot2)
library(png)
library(knitr)
opts_chunk$set(out.width='900px', dpi=200)
資料來自 這份 gist
pokemon_df <- read.csv("data/pokemons.csv", stringsAsFactors = F) %>%
tbl_df %>%
mutate(PokedexHeightCM = PokedexHeightM*100)
pokemon_df %>% glimpse()
## Observations: 151
## Variables: 32
## $ PkMn <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13...
## $ Identifier <chr> "Bulbasaur", "Ivysaur", "Venusaur", "Char...
## $ EvolvesFrom <chr> "0", "Bulbasaur", "Ivysaur", "0", "Charma...
## $ EvolvesTo <chr> "Ivysaur", "Venusaur", "0", "Charmeleon",...
## $ EvoChainID <int> 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5,...
## $ EvoStage <int> 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,...
## $ EvolutionPips <chr> "NORMAL", "NORMAL", "NORMAL", "NORMAL", "...
## $ BaseStamina <int> 90, 120, 160, 78, 116, 156, 88, 118, 158,...
## $ BaseAttack <int> 126, 156, 198, 128, 160, 212, 112, 144, 1...
## $ BaseDefense <int> 126, 158, 200, 108, 140, 182, 142, 176, 2...
## $ Type1 <chr> "GRASS", "GRASS", "GRASS", "FIRE", "FIRE"...
## $ Type2 <chr> "POISON", "POISON", "POISON", "NONE", "NO...
## $ BaseCaptureRate <dbl> 0.16, 0.08, 0.04, 0.16, 0.08, 0.04, 0.16,...
## $ BaseFleeRate <dbl> 0.10, 0.07, 0.05, 0.10, 0.07, 0.05, 0.10,...
## $ CollisionRadiusM <dbl> 0.38, 0.32, 0.76, 0.16, 0.26, 0.41, 0.23,...
## $ CollisionHeightM <dbl> 0.65, 0.64, 1.03, 0.47, 0.77, 1.01, 0.38,...
## $ CollisionHeadRadiusM <dbl> 0.27, 0.25, 0.38, 0.16, 0.23, 0.20, 0.19,...
## $ MovementType <chr> "JUMP", "JUMP", "JUMP", "JUMP", "JUMP", "...
## $ MovementTimerS <int> 10, 23, 11, 29, 23, 11, 10, 23, 14, 10, 3...
## $ JumpTimeS <dbl> 1.15, 1.50, 1.25, 1.25, 1.00, 1.00, 1.00,...
## $ AttackTimerS <int> 29, 8, 4, 10, 8, 4, 29, 8, 5, 29, 3600, 1...
## $ QuickMoves <chr> "Vine Whip, Tackle", "Razor Leaf, Vine Wh...
## $ CinematicMoves <chr> "Sludge Bomb, Seed Bomb, Power Whip", "Sl...
## $ PokemonClass <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ PokedexHeightM <dbl> 0.7, 1.0, 2.0, 0.6, 1.1, 1.7, 0.5, 1.0, 1...
## $ PokedexWeightKg <dbl> 6.9, 13.0, 100.0, 8.5, 19.0, 90.5, 9.0, 2...
## $ HeightStdDev <dbl> 0.09, 0.13, 0.25, 0.08, 0.14, 0.21, 0.06,...
## $ WeightStdDev <dbl> 0.86, 1.63, 12.50, 1.06, 2.38, 11.31, 1.1...
## $ CandyFamily <chr> "001_BULBASAUR", "001_BULBASAUR", "001_BU...
## $ CandyToEvolve <int> 25, 100, 0, 25, 100, 0, 25, 100, 0, 12, 5...
## $ AnimTime <chr> "6d56d53fdaac2a3f6d56d53f93a9ea3f00000000...
## $ PokedexHeightCM <dbl> 70, 100, 200, 60, 110, 170, 50, 100, 160,...
annotate_img <- function(img, x_center, y_center, width, height) {
annotation_raster(
img,
xmin=x_center-width/2,
xmax=x_center+width/2,
ymin=y_center-height/2,
ymax=y_center+height/2)
}
annotate_icon <- function(pokemonId, x_center, y_center, width, height) {
pokemon_icon <- paste0("icons/", pokemonId, ".png") %>% readPNG
annotate_img(pokemon_icon, x_center, y_center, width, height)
}
plot_pokemons <- function(x_coords, y_coords, icon_size = c(10, 10)) {
mapply(
annotate_icon,
pokemon_df$PkMn,
x_coords,
y_coords,
icon_size[1],
icon_size[2]
)
}
plot_2_axis <- function(x_axis, y_axis, icon_size = c(10, 10)) {
pokemon_df %>%
ggplot(aes_string(x= x_axis, y = y_axis)) +
geom_point() +
plot_pokemons(
pokemon_df[[x_axis]],
pokemon_df[[y_axis]],
icon_size
)
}
plot_2_axis("BaseAttack", "BaseDefense")
plot_2_axis("BaseAttack", "BaseStamina", icon_size=c(10, 20))
plot_2_axis("BaseDefense", "BaseStamina", icon_size = c(10, 25))
在 Pokemon go 裡,神奇寶貝物種間的強度受三樣基礎值支配:攻擊、防禦、耐力。各挑兩軸做成三張圖。越往右上角的代表越強,在最右上角的若非神獸,就是常常待在道館塔上的那些。
ash <- readPNG("images/Ash_Ketchum_DP.png")
banana <- readPNG("images/banana.png")
giraffe <- readPNG("images/giraffe.png")
pokemon_df %>%
ggplot(aes(x = PokedexWeightKg, y = PokedexHeightCM)) +
geom_point() +
scale_x_log10(limits = c(NA,1400)) +
scale_y_log10() +
geom_vline(xintercept=43.1) +
geom_hline(yintercept=165) +
annotate_img(ash, log10(43.1), log10(165), 0.2, 0.2) +
annotate_img(banana, log10(0.15), log10(20), 0.2, 0.1) +
annotate_img(giraffe, log10(828), log10(517), 0.2, 0.2) +
plot_pokemons(
log10(pokemon_df$PokedexWeightKg),
log10(pokemon_df$PokedexHeightCM),
icon_size = c(0.1, 0.1)) +
annotate("text", x = 43.1, y = 250, label = "Ash 165cm, 43Kg") +
annotate("text", x = 210, y = 1100, label = "Onix 880cm, 210Kg") +
annotate("text", x = 850, y = 800, label = "Giraffe ♀ \n517cm, 828Kg") +
annotate("text", x= 0.15, y = 220, label = "Haunter\n160cm, 0.1Kg") +
annotate("text", x= 0.15, y = 25, label = "Banana\n20cm, 0.15Kg")